package com.tledu.production.controller.plan;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.plan.CustomerManagement;
import com.tledu.production.model.plan.OrderManagement;
import com.tledu.production.service.plan.PlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/CustomController")
public class CustomerController {
    private final PlanService planService;


//    @GetMapping(value = {"/plan/CustomerManagement"})
//    public String getAll(Model model) {
//        List<CustomerManagement> getAll = planService.getAll();
//        model.addAttribute("getAll", getAll);
//        return "plan/CustomerManagement";
//    }

    @GetMapping(value = {"/plan/CustomerManagement"})
    public String getAllC(Model model, Integer pageNum, Integer pageSize,String condition,String conditionName){
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CustomerManagement> customerManagementList=null;
        PageInfo<CustomerManagement> page=null;
        if (conditionName==null||"".equals(conditionName)){
            customerManagementList = planService.getAllC();
            page = PageInfo.of(customerManagementList);//????????????????????????????
            model.addAttribute("getAllC", page.getList());
            model.addAttribute("page", page);
            model.addAttribute("pages", new int[page.getPages()]);
            return "plan/CustomerManagement";
        } else{
            customerManagementList=planService.search(condition,conditionName);
            model.addAttribute("getAllC",customerManagementList);
            return "plan/CustomerManagement";
        }
    }
//    @GetMapping(value = {"/plan/CustomerManagement"})
//    public String getAllC(Model model, Integer pageNum, Integer pageSize){
//        if (pageNum == null) {
//            pageNum = 1;
//        }
//        if (pageSize == null) {
//            pageSize = 4;
//        }
//        PageHelper.startPage(pageNum, pageSize);
//        List<CustomerManagement> departmentList = planService.getAllC();
//        PageInfo<CustomerManagement> page = PageInfo.of(departmentList);//????????????????????????????
//        model.addAttribute("getAllC", page.getList());
//        model.addAttribute("page", page);
//        model.addAttribute("pages", new int[page.getPages()]);
//        return "plan/CustomerManagement";
//    }
    @GetMapping("/plan/custom-add")
    public String add(){
        return "plan/add/custom-add";
    }

    @PostMapping("/plan/custom-add")
    @ResponseBody
    public AjaxResult<Void> addC(CustomerManagement customerManagement){
        planService.insertC(customerManagement);
        return new AjaxResult<>();

    }
    //************************编辑*****************************
    @GetMapping("/editC/{customId}")
    public String editC(@PathVariable("customId") Integer customId , Model model){
        CustomerManagement customerManagement=planService.editC(customId);
        model.addAttribute("customerManagement",customerManagement);
        return "plan/edit/custom-edit";
    }
    @ResponseBody
    @PostMapping("/update")
    public AjaxResult<Void> updateC(CustomerManagement customerManagement){
        planService.updateC(customerManagement);
        return new AjaxResult<>();
    }
    //************************查找*****************************




    //富文本
    @GetMapping("/richTextC/{customerId}")
    public String richTextO(Model model,  @PathVariable Integer customerId){
        CustomerManagement customerManagement=planService.editC(customerId);
        model.addAttribute("customerManagement",customerManagement);
        return "plan/richText/CustomerRichText";
    }
}
